﻿namespace KeRui.BLL
{
    using KeRui.Common;
    using KeRui.DAL;
    using KeRui.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class Ad
    {
        private readonly KeRui.DAL.Ad dal = new KeRui.DAL.Ad();

        public int Add(KeRui.Model.Ad model)
        {
            return this.dal.Add(model);
        }

        public List<KeRui.Model.Ad> DataTableToList(DataTable dt)
        {
            List<KeRui.Model.Ad> list = new List<KeRui.Model.Ad>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    KeRui.Model.Ad item = new KeRui.Model.Ad();
                    if (dt.Rows[i]["AdID"].ToString() != "")
                    {
                        item.AdID = int.Parse(dt.Rows[i]["AdID"].ToString());
                    }
                    if (dt.Rows[i]["SiteID"].ToString() != "")
                    {
                        item.SiteID = int.Parse(dt.Rows[i]["SiteID"].ToString());
                    }
                    item.AdName = dt.Rows[i]["AdName"].ToString();
                    if (dt.Rows[i]["AdWidth"].ToString() != "")
                    {
                        item.AdWidth = int.Parse(dt.Rows[i]["AdWidth"].ToString());
                    }
                    if (dt.Rows[i]["AdHight"].ToString() != "")
                    {
                        item.AdHight = int.Parse(dt.Rows[i]["AdHight"].ToString());
                    }
                    if (dt.Rows[i]["AdType"].ToString() != "")
                    {
                        item.AdType = int.Parse(dt.Rows[i]["AdType"].ToString());
                    }
                    if (dt.Rows[i]["CurID"].ToString() != "")
                    {
                        item.CurID = int.Parse(dt.Rows[i]["CurID"].ToString());
                    }
                    item.CurUrl = dt.Rows[i]["CurUrl"].ToString();
                    if (dt.Rows[i]["CurSellerID"].ToString() != "")
                    {
                        item.CurSellerID = int.Parse(dt.Rows[i]["CurSellerID"].ToString());
                    }
                    item.CurSellerName = dt.Rows[i]["CurSellerName"].ToString();
                    item.CurAdText = dt.Rows[i]["CurAdText"].ToString();
                    if (dt.Rows[i]["CurStartDate"].ToString() != "")
                    {
                        item.CurStartDate = DateTime.Parse(dt.Rows[i]["CurStartDate"].ToString());
                    }
                    if (dt.Rows[i]["CurEndDate"].ToString() != "")
                    {
                        item.CurEndDate = DateTime.Parse(dt.Rows[i]["CurEndDate"].ToString());
                    }
                    if (dt.Rows[i]["BuyNum"].ToString() != "")
                    {
                        item.BuyNum = int.Parse(dt.Rows[i]["BuyNum"].ToString());
                    }
                    if (dt.Rows[i]["BuyUnit"].ToString() != "")
                    {
                        item.BuyUnit = int.Parse(dt.Rows[i]["BuyUnit"].ToString());
                    }
                    if (dt.Rows[i]["IsUsing"].ToString() != "")
                    {
                        if ((dt.Rows[i]["IsUsing"].ToString() == "1") || (dt.Rows[i]["IsUsing"].ToString().ToLower() == "true"))
                        {
                            item.IsUsing = true;
                        }
                        else
                        {
                            item.IsUsing = false;
                        }
                    }
                    if (dt.Rows[i]["State"].ToString() != "")
                    {
                        item.State = int.Parse(dt.Rows[i]["State"].ToString());
                    }
                    if (dt.Rows[i]["CreateTime"].ToString() != "")
                    {
                        item.CreateTime = DateTime.Parse(dt.Rows[i]["CreateTime"].ToString());
                    }
                    item.CreateIP = dt.Rows[i]["CreateIP"].ToString();
                    item.DefaultAdText = dt.Rows[i]["DefaultAdText"].ToString();
                    item.PersonInCharge = dt.Rows[i]["PersonInCharge"].ToString();
                    list.Add(item);
                }
            }
            return list;
        }

        public int Delete(int AdID)
        {
            return this.dal.Delete(AdID);
        }

        public List<KeRui.Model.Ad> GetList(int siteID)
        {
            string strWhere = " siteID=" + siteID + "";
            string strOrder = " AdName ";
            int pageSize = -1;
            int pageIndex = -1;
            DataSet set = new KeRui.DAL.Common().GetList("Ad", pageSize, pageIndex, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public DataTable GetListAll()
        {
            string strWhere = "";
            string strOrder = " AdID ";
            int pageSize = -1;
            int pageIndex = -1;
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetList("Ad", pageSize, pageIndex, strWhere, strOrder).Tables[0];
        }

        public KeRui.Model.Ad GetModel(int AdID)
        {
            return this.dal.GetModel(AdID);
        }

        public KeRui.Model.Ad GetModelByCache(int AdID)
        {
            string cacheKey = "AdModel-" + AdID;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.dal.GetModel(AdID);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("ModelCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (KeRui.Model.Ad) cache;
        }

        public DataSet GetSelect(int siteID)
        {
            string strWhere = " siteID = " + siteID + " ";
            string strOrder = " AdName asc";
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetSelect("Ad", strWhere, strOrder, " AdID,AdName");
        }

        public int Update(KeRui.Model.Ad model)
        {
            return this.dal.Update(model);
        }

        public int UpdateForSellerAd(int AdID)
        {
            return this.dal.UpdateForSellerAd(AdID);
        }
    }
}

